﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the G.O.S.E(Game Online Server Engine) for .NET.
//
//                 2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Demo_G.O.S.E.ServerEngine.Common;
#endregion

namespace Demo_G.O.S.E.ServerEngine.Network.DLL
{
    /// <summary>
    /// 服务端处的客户端句柄
    /// </summary>
    [MultiThreadedSupportgAttribute( "当前的类所有成员都可锁定,支持多线程操作" )]
    public class ServiceHandle
    {
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 服务端处的客户端句柄指针
        /// </summary>
        private IntPtr m_Value                                                  = IntPtr.Zero;
        /// <summary>
        /// 服务端处的客户端管理
        /// </summary>
        private ServiceHandleManager m_SocketHandlerManager          = null;

        /// <summary>
        /// 处理数据回调函数
        /// </summary>
        internal DelegateClientHandlerProcessMessageBlock m_ProcessMessageBlock = null;
        /// <summary>
        /// 断开回调函数
        /// </summary>
        internal DelegateClientHandlerDisconnect m_Disconnect                   = null;
        #endregion

        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose
        /// <summary>
        /// 初始化构造
        /// </summary>
        public ServiceHandle()
        {
            m_ProcessMessageBlock = new DelegateClientHandlerProcessMessageBlock( this.OnProcessMessageBlock );
            m_Disconnect = new DelegateClientHandlerDisconnect( this.OnDisconnect );
        }
        #endregion

        #region zh-CHS 共有属性 | en Public Properties
        /// <summary>
        /// 客户端的管理句柄
        /// </summary>
        public ServiceHandleManager SocketHandlerManager
        {
            get { return m_SocketHandlerManager; }
            set { m_SocketHandlerManager = value; }
        }

        /// <summary>
        /// 是否在线
        /// </summary>
        public bool IsOnline
        {
            get { return ServiceHandle.ServiceHandle_IsOnline(m_Value); }
        }

        /// <summary>
        /// 远程端口
        /// </summary>
        public int RemotePort
        {
            get { return ServiceHandle.ServiceHandle_RemotePort(m_Value); }
        }

        /// <summary>
        /// 第一次数据包的时间
        /// </summary>
        public long FirstTime
        {
            get { return ServiceHandle.ServiceHandle_FirstTime(m_Value); }
        }

        /// <summary>
        /// 远程的地址
        /// </summary>
        public string RemoteOnlyIP
        {
            get { return ServiceHandle.ServiceHandle_RemoteOnlyIP(m_Value); }
        }

        /// <summary>
        /// 在线时间
        /// </summary>
        public long OnlineTime
        {
            get { return ServiceHandle.ServiceHandle_OnlineTime(m_Value); }
        }

        /// <summary>
        /// 客户段的地址与端口
        /// </summary>
        public string ClientAddress
        {
            get { return ServiceHandle.ServiceHandle_ClientAddress(m_Value); }
        }

        /// <summary>
        /// 消息包的总数量
        /// </summary>
        public long MessageBlockNumbers
        {
            get { return ServiceHandle.ServiceHandle_MessageBlockNumbers(m_Value); }
        }

        /// <summary>
        /// 60秒内的数据包的数量
        /// </summary>
        public long MessageBlockNumbers60sec
        {
            get { return ServiceHandle.ServiceHandle_MessageBlockNumbers60sec(m_Value); }
        }

        /// <summary>
        /// 最后数据包的时间
        /// </summary>
        public long LastMessageBlockTime
        {
            get { return ServiceHandle.ServiceHandle_LastMessageBlockTime(m_Value); }
        }

        /// <summary>
        /// 数据包间隔的时间
        /// </summary>
        public long MessageBlockSpacingTime
        {
            get { return ServiceHandle.ServiceHandle_MessageBlockSpacingTime(m_Value); }
        }
        #endregion

        #region zh-CHS 共有方法 | en Public Methods
        /// <summary>
        /// 关闭连接
        /// </summary>
        public void CloseSocket()
        {
            ServiceHandle.ServiceHandle_CloseSocket(m_Value);
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="sendMessageBlock"></param>
        public void SendTo(MessageBlock sendMessageBlock)
        {
            ServiceHandle.ServiceHandle_SendTo(sendMessageBlock.Value, m_Value);
        }
        #endregion

        #region zh-CHS 共有事件 | en Public Event
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<MessageBlockEventArgs> m_ThreadEventProcessMessageBlock;
        /// <summary>
        /// 
        /// </summary>
        private object m_LockThreadEventProcessMessageBlock = new object();
        #endregion
        /// <summary>
        /// 处理数据的事件
        /// </summary>
        public event EventHandler<MessageBlockEventArgs> ThreadEventProcessMessageBlock
        {
            add { lock ( m_LockThreadEventProcessMessageBlock ) { m_ThreadEventProcessMessageBlock += value; } }
            remove { lock ( m_LockThreadEventProcessMessageBlock ) { m_ThreadEventProcessMessageBlock -= value; } }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<EventArgs> m_ThreadEventDisconnect;
        /// <summary>
        /// 
        /// </summary>
        private object m_LockThreadEventDisconnect = new object();
        #endregion
        /// <summary>
        /// 断开的事件
        /// </summary>
        public event EventHandler<EventArgs> ThreadEventDisconnect
        {
            add { lock ( m_LockThreadEventDisconnect ) { m_ThreadEventDisconnect += value; } }
            remove { lock ( m_LockThreadEventDisconnect ) { m_ThreadEventDisconnect -= value; } }
        }
        #endregion

        #region zh-CHS 内部委托 | en Internal Delegate
        /// <summary>
        /// 处理数据的委托
        /// </summary>
        /// <param name="pRecvMessageBlock"></param>
        internal delegate void DelegateClientHandlerProcessMessageBlock(IntPtr pRecvMessageBlock);
        /// <summary>
        /// 断开的委托
        /// </summary>
        internal delegate void DelegateClientHandlerDisconnect();
        #endregion

        #region zh-CHS 私有的事件处理函数 | en Private Event Handlers
        /// <summary>
        /// 处理数据
        /// </summary>
        /// <param name="pRecvMessageBlock"></param>
        private void OnProcessMessageBlock(IntPtr pRecvMessageBlock)
        {
            EventHandler<MessageBlockEventArgs> tempEvent = m_ThreadEventProcessMessageBlock;
            if ( tempEvent != null )
            {
                MessageBlock recvMessageBlock = new MessageBlock();
                if (recvMessageBlock == null)
                {
                    Debug.WriteLine( "ServiceHandle.OnProcessMessageBlock(...) - recvMessageBlock == null error!" );

                    return;
                }
                recvMessageBlock.Value = pRecvMessageBlock;

                MessageBlockEventArgs messageBlockEventArgs = new MessageBlockEventArgs( recvMessageBlock );
                tempEvent( this, messageBlockEventArgs );
            }
        }

        /// <summary>
        /// 断开
        /// </summary>
        private void OnDisconnect()
        {
            EventHandler<EventArgs> tempEvent = m_ThreadEventDisconnect;
            if ( tempEvent != null )
                tempEvent( this, EventArgs.Empty );
        }
        #endregion

        #region zh-CHS 内部属性 | en Internal Properties
        /// <summary>
        /// 服务端处的客户端句柄指针
        /// </summary>
        protected internal IntPtr Value
        {
            get { return m_Value; }
            set { m_Value = value; }
        }
        #endregion

        #region zh-CHS 引入DLL接口 | en DLL Import
        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern bool ServiceHandle_IsOnline(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern int ServiceHandle_RemotePort(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern long ServiceHandle_FirstTime(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        private static extern string ServiceHandle_RemoteOnlyIP(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern long ServiceHandle_OnlineTime(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        private static extern string ServiceHandle_ClientAddress(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern long ServiceHandle_MessageBlockNumbers(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern long ServiceHandle_MessageBlockNumbers60sec(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern long ServiceHandle_LastMessageBlockTime(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern long ServiceHandle_MessageBlockSpacingTime(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pSendMessageBlock"></param>
        /// <param name="pClientHandlerAtServer"></param>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void ServiceHandle_SendTo(IntPtr pSendMessageBlock, IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void ServiceHandle_CloseSocket(IntPtr pClientHandlerAtServer);
        #endregion
    }
}
#endregion

